package com.ruoyi.project.site.controller;

import cn.hutool.http.HttpUtil;
import com.alibaba.fastjson2.JSON;
import com.alibaba.fastjson2.JSONArray;
import com.alibaba.fastjson2.JSONObject;
import com.ruoyi.framework.web.domain.AjaxResult;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.Map;
import java.util.logging.Logger;

/**
 * @title : GaoDeMapController
 * @Author : Administrator
 * @Date : 2025/4/18
 * @Version : 1.0.0
 * @Description :
 */
@RestController
@RequestMapping("/gaode/map")
public class GaoDeMapController {

    private Logger logger = Logger.getLogger(GaoDeMapController.class.getName());
    String key = "6aa9fe8b1b2478d60a8c66aa58160af7";
//    String key = "87ef0a2cc8ef04c06c757fef9e0340cb";

    /**
     * 获取地址
     * @param point
     * @return
     */
    @GetMapping("/getAddress")
    public AjaxResult getAddress(String point) {
        return AjaxResult.success(getGDAddress(point));
    }
    public Map<String, String> getGDAddress(String point) {
        String url = "https://restapi.amap.com/v3/geocode/regeo?key=" + key + "&location="+point;
        // 请求高德地图API获取经纬度
        logger.info("getAddress url: " + url);
        String result = HttpUtil.get(url);
        // 返回结果示例:{"status":"1","regeocode":{"addressComponent":{"city":[],"province":"北京市","adcode":"110105","district":"朝阳区","towncode":"110105026000","streetNumber":{"number":"6号","location":"116.482086,39.990496","direction":"东","distance":"51.0778","street":"阜通东大街"},"country":"中国","township":"望京街道","businessAreas":[{"location":"116.470293,39.996171","name":"望京","id":"110105"},{"location":"116.494356,39.971563","name":"酒仙桥","id":"110105"},{"location":"116.492891,39.981321","name":"大山子","id":"110105"}],"building":{"name":"方恒国际中心B座","type":"商务住宅;楼宇;商务写字楼"},"neighborhood":{"name":"方恒国际中心","type":"商务住宅;楼宇;商住两用楼宇"},"citycode":"010"},"formatted_address":"北京市朝阳区望京街道方恒国际中心B座"},"info":"OK","infocode":"10000"}
        logger.info("getAddress result: " + result);
        // 返回结果示例: {"status":"1","regeocode":{"addressComponent":{"city":[],"province":"北京市","adcode":"110105","district":"朝阳区","towncode":"110105026000","streetNumber":{"number":"6号","location":"116.482086,39.990496","direction":"东","distance":"51.0778","street":"阜通东大街"},"country":"中国","township":"望京街道","businessAreas":[{"location":"116.470293,39.996171","name":"望京","id":"110105"},{"location":"116.494356,39.971563","name":"酒仙桥","id":"110105"},{"location":"116.492891,39.981321","name":"大山子","id":"110105"}],"building":{"name":"方恒国际中心B座","type":"商务住宅;楼宇;商务写字楼"},"neighborhood":{"name":"方恒国际中心","type":"商务住宅;楼宇;商住两用楼宇"},"citycode":"010"},"formatted_address":"北京市朝阳区望京街道方恒国际中心B座"},"info":"OK","infocode":"10000"}
        JSONObject json = JSON.parseObject(result);
        if (json.getInteger("status") == 1) {
            // 解析返回结果
            String address = json.getJSONObject("regeocode").getString("formatted_address");
            HashMap<String, String> map = new HashMap<>();
            map.put("address", address);
            map.put("location", point);
            return map;
        }
        return  new HashMap<>();
    }

    /**
     * 获取坐标点
     * @param address
     * @return
     */

    @GetMapping("/getPoint")
    public AjaxResult getPoint(String address) {
        // 请求高德地图API获取经纬度
//        String result = HttpUtil.get(url + address);
        String result = HttpUtil.get("https://restapi.amap.com/v3/geocode/geo?address="+address+"&key="+key);
        logger.info("getPoint result: " + result);
        // 返回结果示例: {"status":"1","info":"OK","infocode":"10000","count":"1","geocodes":[{"formatted_address":"山西省晋城市","country":"中国","province":"山西省","citycode":"0356","city":"晋城市","district":[],"township":[],"neighborhood":{"name":[],"type":[]},"building":{"name":[],"type":[]},"adcode":"140500","street":[],"number":[],"location":"112.852022,35.491315","level":"市"}]}
        JSONObject json = JSON.parseObject(result);
        if (json.getInteger("status") == 1 && json.getIntValue("count") > 0) {
            // 解析返回结果
            JSONArray geocodes = json.getJSONArray("geocodes");
            JSONObject geo = geocodes.getJSONObject(0);
            String location_map = geo.getString("location");
            HashMap<String, Object> map = new HashMap<>();
            map.put("address", geo.getString("formatted_address"));
            map.put("location", location_map);
            return AjaxResult.success(map);
        }
        return AjaxResult.error("获取失败");
    }
}
